package com.gugee.ins.data.task.mark.service;

import com.gugee.ins.common.dto.RankTaskParam;
import com.gugee.ins.common.dal.service.mark.InsRkBloggerFgDayDao;
import com.xiguaji.boot.toolkit.DateTimeUtils;
import com.xxl.job.core.log.XxlJobLogger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;


/**
 * ins大博主分类、粉丝增长数日排行表
 * @author huangxin
 */
@Component
@Slf4j
public class InsRkBloggerFgDaySvc extends AbstractMarkRankProducerHandler {

    /**
     *
     */
    @Autowired
    InsRkBloggerFgDayDao insRkBloggerFgDayDao;

    /**
     * 处理ins大博主分类、粉丝增长数日排行表
     */
    @Override
    public void handleRank(RankTaskParam taskParam) {
        // 查找一级分类
        List<String> categoryList = getCategory(taskParam.getCategory(),true);

        long now = getDateTime(taskParam.getDateCode()) / 1000 - 24 * 3600;

        int today = Integer.parseInt(DateTimeUtils.formatSecond(now,"yyyyMMdd",
                taskConfig.getInsBloggerMarkDaytimeZone()));
        int yesterday = Integer.parseInt(DateTimeUtils.formatSecond(now - 24 * 3600,"yyyyMMdd",
                taskConfig.getInsBloggerMarkDaytimeZone()));

        // 分类的日榜
        if(categoryList != null && !categoryList.isEmpty()){
            for(String category : categoryList){
                markRank(today,yesterday,category);
            }
        }
    }

    /**
     *
     * @param dateCode
     * @param lastDate
     * @param category
     */
    public void markRank(int dateCode,int lastDate,String category){
        try{
            insRkBloggerFgDayDao.insertRkBloggerFgDay(dateCode,lastDate,category);
        }catch (Exception e){
            log.info("ins大博主分类、粉丝增长数日榜单 生成失败 date:{},category:{},msg:{}", dateCode,category,e.getMessage());
            XxlJobLogger.log("ins大博主分类、粉丝增长数日榜单 生成失败 date:{},category:{},msg:{}",dateCode,category,e.getMessage());
        }
    }
}
